package com.mall.autoconfig.template;

import com.baidu.aip.face.AipFace;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;


public class AipFaceTemplate {

    @Autowired
    private AipFace client;

    /**
     * 检测图片中是否包含人脸
     *  true：包含
     *  false：不包含
     */
    public boolean detect(String imageUrl) {


        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        HashMap<String, String> options = new HashMap<String, String>();
        options.put("face_field", "age");
        options.put("max_face_num", "2");
        options.put("face_type", "LIVE");
        options.put("liveness_control", "LOW");



        // 调用接口
        //String image = "https://tanhua-zzj.oss-cn-beijing.aliyuncs.com/2022-03-17/681d0d1d-9097-4c91-9f84-bcae9221c5a3.jpg";
        String imageType = "URL"; //图片的解析参数

        // 人脸检测
        JSONObject res = client.detect(imageUrl, imageType, options);//图片 图片解析信息
        System.out.println(res.toString(2));


        boolean flag = false;
        if ("0".equals(res.get("error_code"))) {
            flag =true;
        }
        return flag;
    }
}
